package com.king.modules.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.king.framework.base.service.impl.YyBaseServiceImpl;
import com.king.modules.system.domain.SysUserRoleEntity;
import com.king.modules.system.mapper.SysUserRoleMapper;
import com.king.modules.system.service.SysUserRoleService;

/**
 * 用户 业务层处理
 * 
 * @author king
 */
@Service
public class SysUserRoleServiceImpl extends YyBaseServiceImpl<SysUserRoleMapper, SysUserRoleEntity, Long>
		implements SysUserRoleService {


	@Autowired
	private SysUserRoleMapper userRoleMapper;

	@Override
	public int countUserRoleByRoleId(Long roleId) {
		return userRoleMapper.countUserRoleByRoleId(roleId);
	}

	@Override
	public void deleteUserRoleByUserId(Long userId) {
		userRoleMapper.deleteUserRoleByUserId(userId);
	}

	@Override
	public void deleteUserRoleByUserIds(Long[] userIds) {
		userRoleMapper.deleteUserRoleByUserIds(userIds);
	}

	@Override
	public int insertAuthUsers(Long roleId, Long[] userIds) {
		// 新增用户与角色管理
		List<SysUserRoleEntity> list = new ArrayList<SysUserRoleEntity>();
		for (Long userId : userIds) {
			SysUserRoleEntity ur = new SysUserRoleEntity();
			setBaseEntity(ur);
			ur.setUserId(userId);
			ur.setRoleId(roleId);
			list.add(ur);
		}
		return saveBatch(list) ? list.size() : 0;
	}

	@Override
	public int deleteAuthUsers(Long roleId, Long[] userIds) {
		return userRoleMapper.deleteAuthUsers(roleId, userIds);
	}

	@Override
	public int deleteAuthRoles(Long userId, Long[] roleIds) {
		return userRoleMapper.deleteAuthRoles(userId, roleIds);
	}

	@Override
	public int deleteByRoleId(Long roleId) {
		return userRoleMapper.deleteByRoleId(roleId);
	}

	
	@Override
	public int insertAuthRoles(Long userId, Long[] roleIds) {
		// 新增用户与角色管理
		List<SysUserRoleEntity> list = new ArrayList<SysUserRoleEntity>();
		for (Long roleId : roleIds) {
			SysUserRoleEntity ur = new SysUserRoleEntity();
			setBaseEntity(ur);
			ur.setUserId(userId);
			ur.setRoleId(roleId);
			list.add(ur);
		}
		return saveBatch(list) ? list.size() : 0;
	}

	
}
